<?php
namespace src\logic;
use \classlib\core as C;
class lrank {
	static $filter_user = array(249);
	
	//之定义排序算法，先按照星币排序，相等的按照等级排序
	public static function lv_sort($item1, $item2) {
		if ($item1['coin'] > $item2['coin']) {
			return -1;
		} else if ($item1['coin'] < $item2['coin']) {
			return 1;
		} else {
			return ($item1['lv'] >= $item2['lv'])? -1:1;
		}
	}
	
	//获取主播排行版数据 日-周-月-总
	public static function top_star($fields, $where, $sort, $limit=8) {
		$sorm   = C\model::load('main.pl.studio');
		$filter = $sorm->get_column(0, -1, array('status'=>0), 'userid');//排除官方的主播号
		$filter = empty($filter)? self::$filter_user:array_merge($filter, self::$filter_user);
		$where[]= array('userid', $filter, \classlib\orm::OP_NOTIN);
		$list   = C\model::load('main.pl.rank')->get_list(0, $limit, $where, $fields, $sort, \classlib\orm::DESC);
		if ($list) {
			$uorm = C\model::load('main.pl.userinfo');
			foreach ($list as &$item) {
				$studio= $sorm->get_one($item['refid']);
				$user  = $uorm->set_dev($item['userid'])->get_one($item['userid']);
				$item['lv']       = ($user['lv'] < 1)? 1:$user['lv'];
				$item['lvicon']   = lv::icon($user['lv'], 0);
				$item['nickname'] = $user['nickname'];
				$item['avtar']    = $user['thumb'];
				$item['link']     = C\router::get_url('index', 'linkto', array('sid'=>$studio['id']));
				$item['starlv']   = lstudio::grade($studio['starlv']);
			}
			usort($list, '\src\logic\lrank::lv_sort');
		} else {
			$list = array();
		}
		return $list;
	}
	
	//获取会员排行版数据 日-周-月-总
	public static function top_viewer($fields, $where, $sort, $limit=8) {
		$list = C\model::load('main.pl.rank')->get_list(0, $limit, $where, $fields, $sort, \classlib\orm::DESC);
		if ($list) {
			$uorm = C\model::load('main.pl.userinfo');
			foreach ($list as &$item) {
				$user  = $uorm->set_dev($item['userid'])->get_one($item['userid']);
				$item['nickname'] = $user['nickname'];
				$item['avtar']    = $user['thumb'];
				$item['lvicon']   = lv::icon($user['lv'], 1);
				$item['lv']       = ($user['lv'] < 1)? 1:$user['lv'];
			}
			usort($list, '\src\logic\lrank::lv_sort');
		} else {
			$list = array();
		}
		return $list;
	}
}